package com.talent.jiwen_teacher.my;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.talent.jiwen_teacher.IndexActivity;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.ui.widgets.StarBar;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

import static com.talent.jiwen_teacher.course.CourseWaitFragment.REFRESH_WAIT_COURSE;

public class EvaluateActivity extends BaseActivity {

    @BindView(R.id.submit)
    Button submitTv;
    @BindView(R.id.teacherIv)
    ImageView teacherIv;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.useTimeTv)
    TextView useTimeTv;
    @BindView(R.id.effectRatingBar)
    StarBar effectRatingBar;
    @BindView(R.id.serviceRatingBar)
    StarBar serviceRatingBar;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_evaluate;
    }

    @Override
    public String titleName() {
        return "课程评价";
    }

    String roomId;
    @Override
    protected void initData() {

        roomId=getIntent().getStringExtra("RoomId");
        if(!Validators.isEmpty(roomId)){

        }

        mBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intentActivity(IndexActivity.class);
                EventBus.getDefault().post(REFRESH_WAIT_COURSE);
                finish();
            }
        });

        effectRatingBar.setIntegerMark(true);
        serviceRatingBar.setIntegerMark(true);


        ImageUtil.loadCircleImage(EvaluateActivity.this,SPUtil.getUserHeadImg(),teacherIv);
        nameTv.setText(SPUtil.getUserName());
        useTimeTv.setText("请对参与本次课程的学生评分");


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mBtnLeft.performClick();
    }

    @OnClick({R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:
                attitudeScore= (int) effectRatingBar.getStarMark();
                punctualScore= (int) serviceRatingBar.getStarMark();
                saveTeacherScoreInfo();
                break;
        }
    }

    private int attitudeScore;//态度
    private int punctualScore;//守时

    private void saveTeacherScoreInfo() {

        Map<String, String> map = new HashMap<>();
        map.put("orderId", roomId);
        map.put("teacherId", SPUtil.getUserId());
//        map.put("studentId", ""+workEndPosition);
        map.put("attitudeScore", ""+attitudeScore);
        map.put("punctualScore", ""+punctualScore);

        Observable observable = ApiFactory.getEducationApiSingleton().saveTeacherScoreInfo(SPUtil.getToken(),map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(mContext) {
            @Override
            protected void _onNext(Void v) {
                showToast("评论成功");
                intentActivity(IndexActivity.class);
                EventBus.getDefault().post(REFRESH_WAIT_COURSE);
                finish();
            }

            @Override
            protected void _onError(String message) {
//                showToast(message);
                intentActivity(IndexActivity.class);
                finish();
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);

    }

}
